#include <iostream>
#include "skyBox.h"
#include "imageloader.h"


// vertices (static)
const GLfloat skyBox::A[3] = { 0.0f, 1.0f, 1.0f }; 
const GLfloat skyBox::B[3] = { 1.0f, 1.0f, 1.0f }; 
const GLfloat skyBox::C[3] = { 1.0f, 1.0f, 0.0f }; 
const GLfloat skyBox::D[3] = { 0.0f, 1.0f, 0.0f }; 
const GLfloat skyBox::E[3] = { 0.0f, 0.0f, 1.0f }; 
const GLfloat skyBox::F[3] = { 1.0f, 0.0f, 1.0f }; 
const GLfloat skyBox::G[3] = { 1.0f, 0.0f, 0.0f }; 
const GLfloat skyBox::H[3] = { 0.0f, 0.0f, 0.0f }; 


skyBox::skyBox(GLint size, GLint posX, GLint posY, GLint posZ)
{
	this->size = size;  // default = 100
	this->posX = posX;
	this->posY = posY;
	this->posZ = posZ;

	loadTextureImages();
}

void skyBox::loadTextureImages()
{
	//textureFile = "textures\\skybox.bmp";
	//textureFile = "textures\\skybox2.bmp";
	//textureFile = "textures\\high_res\\skybox3.bmp";
	//textureFile = "textures\\high_res\\skybox4.bmp";
	textureFile = "textures\\high_res\\skybox5.bmp";
	imageSkyBox = loadBMP(textureFile.c_str());
	_textureId = loadTexture(imageSkyBox);
	delete imageSkyBox;

	makeDisplayList();
}

GLint skyBox::getSize()
{
	return this->size;
}


void skyBox::makeDisplayList()
{
	// create a new display list
	DLindex = glGenLists(1);
	glNewList(DLindex, GL_COMPILE);

		
		glPushMatrix();
			GLfloat sky_diffuse[] = {0.7, 0.7, 0.7, 1.0};
			GLfloat sky_ambient[] = {0.7, 0.7, 0.7, 1.0};
			GLfloat sky_specular[] = {0.5, 0.5, 0.5, 1.0};

			glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, sky_diffuse);
			glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, sky_ambient );
			glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, sky_specular);
			glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 10.0f);

			glColor3f(1,1,1);
			//glTranslatef(-(size/2) + 25, -(size/2) , -(size/2) +25);
			
			//glScalef(size, size, size);

			// center the platform
			glTranslatef(posX, posY, posZ);
			
			// place at origin
			glTranslatef(-size/2, -size/2, -size/2);
			glScalef(size, size, size);


			// load texture
			glBindTexture(GL_TEXTURE_2D, _textureId);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

			// draw each face...

			//-----------
			// top
			//-----------
			glBegin(GL_QUADS);
				glNormal3f( 0.0, 1.0, 0.0);
				glTexCoord2f( 0, 0);
				glVertex3fv(D);
				//glTexCoord2f( (GLfloat)1/3, 0);
				glTexCoord2f( 0.25, 0);
				glVertex3fv(C);
				//glTexCoord2f( (GLfloat)1/3, 0.5);
				glTexCoord2f( 0.25, 0.5);
				glVertex3fv(B);
				glTexCoord2f( 0, 0.5);
				glVertex3fv(A);
			glEnd();

	
			//-----------
			// bottom
			//-----------
			glBegin(GL_QUADS);
				glNormal3f( 0.0, 1.0, 0.0);
				//glTexCoord2f( (GLfloat)1/3, 0);
				glTexCoord2f( 0.25, 0);
				glVertex3fv(E);
				//glTexCoord2f( 2*(GLfloat)1/3, 0);
				glTexCoord2f( 0.5, 0);
				glVertex3fv(F);
				//glTexCoord2f( 2*(GLfloat)1/3, 0.5);
				glTexCoord2f( 0.5, 0.5);
				glVertex3fv(G);
				//glTexCoord2f( (GLfloat)1/3, 0.5);
				glTexCoord2f( 0.25, 0.5);
				glVertex3fv(H);
			glEnd();


	
			//-----------
			// back
			//-----------
			glBegin(GL_QUADS);
				glNormal3f( 0.0, 0.0, 1.0);
				//glTexCoord2f( 2*(GLfloat)1/3, 0);
				glTexCoord2f( 0.5, 0);
				glVertex3fv(F);
				//glTexCoord2f( 1, 0);
				glTexCoord2f( 0.75, 0);
				glVertex3fv(E);
				//glTexCoord2f( 1, 0.5);
				glTexCoord2f( 0.75, 0.5);
				glVertex3fv(A);
				//glTexCoord2f( 2*(GLfloat)1/3, 0.5);
				glTexCoord2f( 0.5, 0.5);
				glVertex3fv(B);
			glEnd();



			//-----------
			// Front
			//-----------
			glBegin(GL_QUADS);
				glNormal3f( 0.0, 0.0, 1.0);
				//glTexCoord2f( 0, 0.5);
				glTexCoord2f( 0, 0.5);
				glVertex3fv(H);
				//glTexCoord2f( (GLfloat)1/3, 0.5);
				glTexCoord2f( 0.25, 0.5);
				glVertex3fv(G);
				//glTexCoord2f( (GLfloat)1/3, 1);
				glTexCoord2f( 0.25, 1);
				glVertex3fv(C);
				//glTexCoord2f( 0, 1);
				glTexCoord2f( 0, 1);
				glVertex3fv(D);
			glEnd();


			//-----------
			// left
			//-----------
			glBegin(GL_QUADS);
				glNormal3f( 1.0, 0.0, 0.0);
				//glTexCoord2f( (GLfloat)1/3, 0.5);
				glTexCoord2f( 0.25, 0.5);
				glVertex3fv(E);
				//glTexCoord2f( 2*(GLfloat)1/3, 0.5);
				glTexCoord2f( 0.5, 0.5);
				glVertex3fv(H);
				//glTexCoord2f( 2*(GLfloat)1/3, 1);
				glTexCoord2f( 0.5, 1);
				glVertex3fv(D);
				//glTexCoord2f( (GLfloat)1/3, 1);
				glTexCoord2f( 0.25, 1);
				glVertex3fv(A);
			glEnd();


			//-----------
			// right
			//-----------
			glBegin(GL_QUADS);
				glNormal3f( 1.0, 0.0, 0.0);
				//glTexCoord2f( 2*(GLfloat)1/3, 0.5);
				glTexCoord2f( 0.5, 0.5);
				glVertex3fv(G);
				//glTexCoord2f( 1, 0.5);
				glTexCoord2f( 0.75, 0.5);
				glVertex3fv(F);
				//glTexCoord2f( 1, 1);
				glTexCoord2f( 0.75, 1);
				glVertex3fv(B);
				//glTexCoord2f( 2*(GLfloat)1/3, 1);
				glTexCoord2f( 0.5, 1);
				glVertex3fv(C);
			glEnd();
		glPopMatrix();
	glEndList();
}

void skyBox::draw()
{
	

	glCallList(DLindex);

	GLfloat default_diffuse[] = {0.2, 0.2, 0.2, 1.0};
	GLfloat default_ambient[] = {0.3, 0.3, 0.3, 1.0};
	GLfloat default_specular[] = {0.0, 0.0, 0.0, 1.0};

	glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, default_diffuse);
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, default_ambient );
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, default_specular);
    glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 0.0f);

}

skyBox::~skyBox(void)
{
}
